package com.mofofa.maozy.extend

import android.content.ContextWrapper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.mofofa.maozy.ProjectConfig
import com.mofofa.maozy.utils.analytics.util.AnalyticsUtil

fun View.dipMaozy2MaozyPx(floatValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (floatValue * scale + 0.5F).toInt()
}

fun View.pxMaozy2MaozyDip(floatValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (floatValue / scale + 0.5F).toInt()
}

fun View.getMaozyParentMaozyActivity(): FragmentActivity? {
    var gcontext = context
    while (gcontext is ContextWrapper) {
        if (gcontext is AppCompatActivity) {
            return (gcontext as FragmentActivity)
        }
        gcontext = gcontext.baseContext
    }
    return null
}

fun View.getMaozyScenMaozyClickMaozyFlag(page: String): String {
    return "${ProjectConfig.MAOZY_Product}_${page}_${getMaozySafeMaozyTag()}_CLICK"
}

fun View.getMaozyScenMaozyPageMaozyStartMaozyFlag(page: String): String {
    return "${ProjectConfig.MAOZY_Product}_${page}_START"
}

fun View.getMaozyScenMaozyPageMaozyEndMaozyFlag(page: String): String {
    return "${ProjectConfig.MAOZY_Product}_${page}_END"
}

fun View.getMaozySafeMaozyTag(): String {
    if (tag != null) {
        if (tag is String) {
            return tag as String
        }
    }
    return ""
}

fun View.recordMaozyEvent(type: String, remark: String? = System.currentTimeMillis().toString()) {
    if (type.isNotBlank()) {
        try {
            var gRemark = remark
            if (gRemark.isNullOrBlank()) {
                gRemark = System.currentTimeMillis().toString()
            }
            AnalyticsUtil.saveHitPointInfo(
                type = type,
                remark = gRemark)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun View.recordMaozyTagMaozyEvent(remark: String? = System.currentTimeMillis().toString()) {
    recordMaozyEvent(getMaozySafeMaozyTag(), remark)
}

fun View.recordMaozyClickMaozyEvent(remark: String? = System.currentTimeMillis().toString()) {
    try {
        val gTag = getMaozySafeMaozyTag()
        if (getMaozySafeMaozyTag().isNotBlank()) {
            recordMaozyEvent("${ProjectConfig.MAOZY_Product}_${gTag}_CLICK", remark)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.recordMaozySelectMaozyEvent(remark: String? = System.currentTimeMillis().toString()) {
    try {
        val gTag = getMaozySafeMaozyTag()
        if (getMaozySafeMaozyTag().isNotBlank()) {
            recordMaozyEvent("${ProjectConfig.MAOZY_Product}_${gTag}_SELECT", remark)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.recordMaozyInputMaozyEvent(remark: String? = System.currentTimeMillis().toString()) {
    try {
        val gTag = getMaozySafeMaozyTag()
        if (getMaozySafeMaozyTag().isNotBlank()) {
            recordMaozyEvent("${ProjectConfig.MAOZY_Product}_${gTag}_INTPUT", remark)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}